// pages/agentPay/agentPay.js
var app = getApp();
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order_id:'',//订单ID
    type:1,//代付类型1：订单代付 2:续费代付
    lanuch_user_id:'',//发起人用户ID
    name:{},//发起人用户信息，
    avatar_img_all:{},
    orderInfo:{},//订单详情
    accountInfo:{},//经销商账户信息
    paying_agent_deduction_account_balance:0,
    order_no:'',//订单编号
    order_actual_money:'',//订单金额
    create_time:'',//下单时间
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options,'payop')
    if (util.isExitsVariable(options.scene)) {
      var scene = util.sceneToArr(decodeURIComponent(options.scene));
      if (util.isExitsVariable(scene.order_id)) {
        this.setData({
          order_id:scene.order_id,
          type:scene.type,//1：订单代付 2:续费代付
          lanuch_user_id:scene.uid,
          role:scene.r||4
        })
      }
    }
    if (util.isExitsVariable(options.order_id)) {
    this.setData({
      order_id:options.order_id,
      type:options.type,//1：订单代付 2:续费代付
      lanuch_user_id:options.uid,
      role:options.r||4
    })
  }
    if(this.data.type==1){
      this.getOrderInfo();//订单详情
    }else if(this.data.type==2){
      this.getRenewOrderInfo();
    }
    
   
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    if(this.data.type==1){
      this.getOrderInfo();//订单详情
    }else if(this.data.type==2){
      this.getRenewOrderInfo();
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
 // 查询续费订单详情
 getRenewOrderInfo: function () {
  var that = this;
  var order_id = that.data.order_id;  // 订单ID
  app.ajax({
    url: 'User/Renew/getRenewOrderDetail',
    data: { renew_order_id: order_id },
    success: function (res) {
      if (res.data.code == 1000) {
        that.setData({
          orderInfo: res.data.data,
          order_no:res.data.data.renew_order_no,
          order_actual_money:res.data.data.order_actual_money,
          create_time:res.data.data.create_time
        });
      } else {
        app.showToast(res.data.message);
      }
      if(that.data.role==5){
        that.engineersInfo();
      }else{
        that.getUserInfo();
      }

      that.workerInfo();
    }
  })
},
  //查询订单详情
  getOrderInfo:function(){
    var that = this;
    var order_id = that.data.order_id;
    app.ajax({
      url: 'User/Order/getOrderDetail',
      method: "POST",
      data: { order_id: order_id },
      success: function (res) {
        //console.log(res)
        if (res.data.code == 1000) {
          that.setData({
            orderInfo: res.data.data,
            order_no:res.data.data.order_no,
            order_actual_money:res.data.data.order_actual_money,
            create_time:res.data.data.create_time
          });
        } else {
          app.showToast(res.data.message);
        }
        if(that.data.role==5){
          that.engineersInfo();
        }else{
          that.getUserInfo();
        }
        that.workerInfo();
      }
    })
  },
  //获取工程信息
  engineersInfo(){
    var that = this;
    app.ajax({
      url: "User/User/engineersInfo",
      data: {"engineers_id":that.data.lanuch_user_id},
      success: function(res) {
        if (res.data.code == 1000) {
          that.setData({
            name: res.data.data.engineers_name,
            avatar_img_all:res.data.data.avatar_img_all
          })
        } else {
          app.showToast(res.data.message, "none", 2000, function() {});
        }

      }
    })
  },
      //获取用户信息
  getUserInfo() {
    var that = this;
    app.ajax({
      url: "User/User/getUserInfo",
      data: {"user_id":that.data.lanuch_user_id},
      success: function(res) {
        if (res.data.code == 1000) {
          that.setData({
            name: res.data.data.username,
            avatar_img_all:res.data.data.avatar_img_all
          })
        } else {
          app.showToast(res.data.message, "none", 2000, function() {});
        }

      }
    })
  },
   // 获取经销商信息
   workerInfo() {
    var that = this;
    app.ajax({
      url: "User/Withdraw/getAccountInfo",
      data: { role:6 },
      success: function(res) {
        if (res.data.code == 1000) {
          that.setData({
            accountInfo: res.data.data,
          })
        }
      }
    })
  },
  //调起支付
  payment:function(){
    var that = this;
    var pay_way = 1;
    that.setData({
      disabled:true
    })
    var order_id = that.data.order_id;
    var paying_agent_user_openid = wx.getStorageSync('paying_agent_user_openid');
    if(this.data.type==1){
    app.ajax({
      url: 'User/Order/pay',
      method: "POST",
      data: { 
        order_id: order_id,
        pay_way:1,
        is_paying_agent:1,
        paying_agent_deduction_account_balance:that.data.paying_agent_deduction_account_balance,
        paying_agent_role:2,
        paying_agent_user_openid:paying_agent_user_openid
      },
      success: function (res) {
        //console.log(res)
        if (res.data.code == 1000 && (pay_way == 1 || pay_way == 2)) {
          if(res.data.data.money==0){
            that.setData({
              disabled: false
            })           
            wx.navigateTo({
              url: '/pages/agentPaySuccess/agentPaySuccess?order_id='+order_id,
            })
          }else{
          var jsapi = res.data.data.jsapi;
          wx.requestPayment({
            timeStamp: jsapi.timeStamp,
            nonceStr: jsapi.nonceStr,
            package: jsapi.package,
            signType: jsapi.signType,
            paySign: jsapi.paySign,
            'success': function (res) {
              that.setData({
                disabled: false
              })
              wx.navigateTo({
                url: '/pages/agentPaySuccess/agentPaySuccess?order_id=' + order_id,
              })
            },
            'fail': function (res) {
              that.setData({
                disabled: false
              })
              app.showToast("支付失败");
             // console.log(res);
            }
          })
        }
        } else if (res.data.code == 1000 && pay_way==4){
           that.setData({
              disabled: false
            })           
            wx.navigateTo({
              url: '/pages/agentPaySuccess/agentPaySuccess?order_id=' + order_id,
            })
        }else{
          app.showToast(res.data.message);
        }
      }
    })
    }else{//续费支付
      app.ajax({
        url: 'User/Renew/renewOrderPay',
        method: "POST",
        data: { 
          renew_order_id: order_id,
          pay_way: 1,
          is_paying_agent:1,
          paying_agent_deduction_account_balance:that.data.paying_agent_deduction_account_balance,
          paying_agent_role:2,
          paying_agent_user_openid:paying_agent_user_openid
        },
        success: function (res) {
          //console.log(res)
          if (res.data.code == 1000 && (pay_way == 1 || pay_way == 2)) {
            if(res.data.data.money==0){
              that.setData({
                disabled: false
              })           
              wx.navigateTo({
                url: '/pages/agentPaySuccess/agentPaySuccess?order_id='+order_id+'&type='+that.data.type,
              })
            }else{
            var jsapi = res.data.data.jsapi;
            wx.requestPayment({
              timeStamp: jsapi.timeStamp,
              nonceStr: jsapi.nonceStr,
              package: jsapi.package,
              signType: jsapi.signType,
              paySign: jsapi.paySign,
              'success': function (res) {
                that.setData({
                  disabled: false
                })
                wx.navigateTo({
                  url: '/pages/agentPaySuccess/agentPaySuccess?order_id=' + order_id+'&type='+that.data.type,
                })
              },
              'fail': function (res) {
                that.setData({
                  disabled: false
                })
                app.showToast("支付失败");
               // console.log(res);
              }
            })
          }
          } else if (res.data.code == 1000 && pay_way==4){
             that.setData({
                disabled: false
              })           
              wx.navigateTo({
                url: '/pages/agentPaySuccess/agentPaySuccess?order_id=' + order_id+'&type='+that.data.type,
              })
          }else{
            app.showToast(res.data.message);
          }
        }
      })
    }
  },
  //输入金额
  inputMoney(e){
    var paying_agent_deduction_account_balance = e.detail.value;
    this.setData({
      paying_agent_deduction_account_balance:paying_agent_deduction_account_balance
    })
  }
 

})